package com.astudio.lhasa;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astudio.lhasa.model.City;
import com.astudio.lhasa.model.Image;
import com.astudio.lhasa.model.ImageDao;
import com.astudio.lhasa.model.TourSpot;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

public class TourSpotImagesActivity extends MainActivity {
    private City city;
    private TourSpot spot;
    private List<Image> images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        spot = getIntent().getParcelableExtra(TourSpotDetailActivity.TOUR_EXTRA);
        city = getIntent().getParcelableExtra(CityDetail.CITY_EXTRA);

        images = MyApplication.getDaoSession()
                .getImageDao()
                .queryBuilder()
                .where(ImageDao.Properties.Owner.eq(spot.getId()))
                .list();
        setTitle(spot.getName() + "风景图片");
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new RecyclerView.Adapter<VH>() {
            @NonNull
            @Override
            public VH onCreateViewHolder (@NonNull ViewGroup parent, int viewType){
                View v = LayoutInflater.from(TourSpotImagesActivity.this)
                        .inflate(R.layout.tour_spot_image_item, parent, false);
                return new VH(v);
            }

            @Override
            public void onBindViewHolder (@NonNull VH holder,int position){
                holder.bindData(images.get(position));
            }

            @Override
            public int getItemCount () {
                return images.size();
            }
        } ;
    }

    private class VH extends RecyclerView.ViewHolder {
        private View itemView;

        public VH(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public void bindData(final Image image) {
            SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.image);
            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(getResources());
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(300)
                    .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.FIT_CENTER)
                    .build();
            simpleDraweeView.setHierarchy(hierarchy);
            int end = image.getUrl().indexOf("imageMogr2");
            simpleDraweeView.setImageURI(image.getUrl().substring(0, end));
        }
    }
}
